import moment from 'moment/moment';

// 时间转换
export const momentDate = ((value, formatString) => {
	formatString = formatString || 'YYYY-MM-DD HH:mm:ss';
	// return moment(value).format(formatString); // value可以是普通日期 20170723
	return moment.unix(value).format(formatString); // 这是时间戳转时间
})

//时间转时间戳
export const momentTurn = ((value) => {
	let timestamp = parseInt(new Date(value).getTime() / 1000);
	return timestamp;
})


export const yuan = (value) => {
	const valueNum = +value;
	if(!Number.isNaN(valueNum)) {
		value = '￥' + (valueNum / 100).toFixed(2)
	}
	return value;
}

// 正则判断
export const NoNull = function(value) { //非空
	return(value == "" || (/^\s+$/g.test(value))) ? false : true;
}

export const notEmpty = function(value) { // 值不为空且可为0
	return !(typeof value == "undefined" || value == null || value === "")
}
//整数与0
export const Rounding = function(value) {
	let reg = /^([1-9]\d*|[0]{1,1})$/;
	if(reg.test(value)) {
		return true;
	} else {
		return false;
	}
}

//整数
export const Integer = function(value) {
	let reg = /^-?[1-9]\d*|[0]$/;
	if(reg.test(value)) {
		return true;
	} else {
		return false;
	}
}

// 字符串值不为空且不为0
export const isEmpty = function(value) {
	return typeof value == "undefined" || value == null || value == ""
}

export const isExternal = function(path) {
  return /^(https?:|mailto:|tel:)/.test(path)
}

//保留两位小数
export const twoSpace = function(value) {
	const reg = /^(?=.*[1-9])\d+(\.\d{1,2})?$/;
	if(reg.test(value)) {
		return true;
	} else {
		return false;
	}
}
// 验证手机号，只校验位数
export const checkPhone = function(val) {
	const reg = /^1[23456789]\d{9}$/;
	if(!reg.test(val)){
		return false
	} else {
		return true
	}
}

//elementui 表单验证 wwb
export const ele_form_phone = (rule, value, callback) => {
	if (!( /^1[23456789]\d{9}$/.test(value) ) ) {
		callback(new Error('请输入正确11位手机号'));
	} else {
		callback();
	}
}
export const ele_form_phoneOrtel = (rule,value,callback) => {
	if(!value){
		callback();
	} else {
		if (( /^1[3456789]\d{9}$/.test(value) ) || (/^0\d{2,3}-?\d{7,8}$/.test(value)) ) {
			callback();
		} else {
			callback(new Error('请输入正确的电话号码或者手机号码'))
		}
    }
}
/** end */
